package com.spring.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.spring.gowhere.R;
import com.spring.httputils.RequestParams;
import com.spring.httputils.RestClient;
import com.spring.httputils.TextHttpResponseHandler;
import com.spring.model.ContactData;
import com.spring.model.ReturnAccountInfo;
import com.spring.tool.App;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Administrator
 * 
 */
public class MyContactDataActivity extends BaseActivity {
	RestClient restClient = null;

	MyHandler myHandler;
	ReturnAccountInfo accountinfo = null;
	String username = null;
	String logintype = null;
	String location_textStr;
	private RelativeLayout reservation_location;
	private LinearLayout linearLayoutorder;
	private EditText querykq;
	private Button btnlogin;//
	private String stredwebservicedaihao;
	 public List<ContactData> contactList;
	 private ListView xflistview;
	 private LinearLayout morelinearylayout;//
	 Button btnupmore,btndownmore;
	 private int index=1;
	 private RelativeLayout reservation_location01;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mycontact);
		TextView textView = (TextView) findViewById(R.id.title_tv);
		btnlogin =(Button)findViewById(R.id.btnlogin);
		querykq =(EditText)findViewById(R.id.querykq);
		reservation_location =(RelativeLayout)findViewById(R.id.reservation_location);
		morelinearylayout =(LinearLayout)findViewById(R.id.morelinearylayout);
		
		xflistview =(ListView)findViewById(R.id.xflistview);
		btnupmore = (Button) findViewById(R.id.btnupmore);
		btndownmore =(Button)findViewById(R.id.btndownmore);
		textView.setText("一卡通网点查询");
		myHandler = new MyHandler();
		ImageButton imageButton = (ImageButton) findViewById(R.id.title_left_ib);
		imageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		btnlogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				stredwebservicedaihao = querykq.getText().toString();
				if (stredwebservicedaihao != null
						&& !"".equals(stredwebservicedaihao)) {
					restClient = new RestClient(MyContactDataActivity.this);
					SendHttpDate(stredwebservicedaihao);
				}
			}
		});
		
		btnupmore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(index>1){					
					index--;
				}
				// TODO Auto-generated method stub
				SendHttpDate(stredwebservicedaihao);
			}
		});
		
		btndownmore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				index++;
				// TODO Auto-generated method stub
				SendHttpDate(stredwebservicedaihao);
			}
		});
	}

	private void SendHttpDate(String stredwebservicedaihao) {
		String url = "http://120.237.110.50:8022/WebService.asmx/QueryContactList";
		RequestParams requestparams = new RequestParams(url, "POST");
		requestparams.put("ContactCaption", stredwebservicedaihao);
		requestparams.put("PageIndex", ""+index);

		TextHttpResponseHandler textHttpResponseHandler = new TextHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String responseString) {
				// TODO Auto-generated method stub
				Log.e("TAG", "responseString---" + responseString);
				if (responseString != null && !"".equals(responseString)) {
					AnalysisData(responseString);
				}

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				// TODO Auto-generated method stub
				Log.e("TAG", "responseString---" + responseString);
				// AnalysisData(responseString);

			}
		};
		try {
			restClient.post(url, requestparams, textHttpResponseHandler);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * json数据解析
	 * 
	 * @param strResult
	 * @throws JSONException
	 */
	private void AnalysisData(String strResult) {
		/**
		 * 这里需要分析服务器回传的json格式数据，
		 */
		JSONObject jsonObject = null;
		try {
			jsonObject = new JSONObject(strResult);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			JSONArray jsonarray = jsonObject.getJSONArray("ContactList");
			contactList = new ArrayList<ContactData>();
			for (int i = 0; i < jsonarray.length(); i++) {
				JSONObject json = jsonarray.getJSONObject(i);
				ContactData contactData = new ContactData();
				contactData.setContactCaption(json.getString("ContactCaption"));
				contactData.setAddr(json.getString("Addr"));
				contactData.setLinkMan(json.getString("LinkMan"));
				contactData.setLinkTel(json.getString("LinkTel"));
				contactData.setLinkUrl(json.getString("LinkUrl"));
				contactData.setWebSrvUrl(json.getString("WebSrvUrl"));
//				Log.e("sdf", "test------"+json.getString("LinkUrl:::") +"webSrvUrl:::"+json.getString("WebSrvUrl"));
//				App.getInstance().url = json.getString("WebSrvUrl");
				Log.e("sdfsdf", "abcxxq---+"+App.getInstance().url);
				contactList.add(contactData);
			}
			if(contactList!=null&&contactList.size()>0){
				Message msg = new Message();
				msg.arg1 = 1;
				MyContactDataActivity.this.myHandler.sendMessage(msg); // 向Handler发送消息,更新UI
			}else{
				Toast.makeText(MyContactDataActivity.this, "没有查询到相关数据",  Toast.LENGTH_SHORT).show();
			}
		
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 接受消息,处理消息 ,此Handler会与当前主线程一块运行
	 * */

	class MyHandler extends Handler {
		public MyHandler() {
		}

		public MyHandler(Looper L) {
			super(L);
		}

		// 子类必须重写此方法,接受数据
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			Log.d("MyHandler", "handleMessage......");
			super.handleMessage(msg);
			// 此处可以更新UI
			switch (msg.arg1) {
			case 1:
				if (contactList != null&&contactList.size()>0) {
					btnlogin.setVisibility(View.GONE);
					reservation_location.setVisibility(View.GONE);
					xflistview.setVisibility(View.VISIBLE);
					morelinearylayout.setVisibility(View.VISIBLE);
					MessageAdapter adapter = new MessageAdapter(
							MyContactDataActivity.this, contactList);
					xflistview.setAdapter(adapter);
				}

				break;
			case 2:
				break;
			default:
				break;
			}

		}
	}
	
	public class MessageAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private List<ContactData> returnInfoList;

		public MessageAdapter(Context context, List<ContactData> lists) {
			this.mInflater = LayoutInflater.from(context);
			this.returnInfoList = lists;

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (returnInfoList != null) {
				return returnInfoList.size();
			}
			return 0;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.mycontactadapter, null);
				holder.txContactCaption = (TextView) convertView
						.findViewById(R.id.txContactCaption);
				holder.txAddr = (TextView) convertView
						.findViewById(R.id.txAddr);
				holder.txLinkMan = (TextView) convertView
						.findViewById(R.id.txLinkMan);
				holder.txLinkTel = (TextView) convertView
						.findViewById(R.id.txLinkTel);
				holder.txLinkUrl = (TextView) convertView
						.findViewById(R.id.txLinkUrl);

	

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.txContactCaption.setText(returnInfoList.get(position).getContactCaption());
			holder.txAddr.setText(returnInfoList.get(position).getAddr());
			holder.txLinkMan.setText(returnInfoList.get(position).getLinkMan());
			holder.txLinkTel.setText(returnInfoList.get(position).getLinkTel());
			holder.txLinkUrl.setText(returnInfoList.get(position).getLinkUrl());
			return convertView;
		}

		public class ViewHolder {
			TextView txContactCaption, txAddr, txLinkMan, txLinkTel, txLinkUrl,
			WebSrvUrl;

		}

	}

}
